package br.com.biblioteca.ronie.usuario;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.biblioteca.ronie.util.HibernateUtil;

public class UsuarioDAO 
{	
	
	public Usuario verificarLogin(String email, String senha)
	{
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		
		String hql = "FROM Usuario WHERE email = '"+email+"' AND senha = '"+senha+"'";
		System.out.println(hql);
		Query query = session.createQuery(hql);
		Usuario usuarioLogin = (Usuario) query.uniqueResult();
		
		t.commit();
		
		return usuarioLogin;
	}
	
	public void cadastrarUsuario(Usuario usuario)
	{	
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		session.save(usuario);
		t.commit();
		
		if(session.isOpen())
			session.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<Usuario> listarTodos()
	{	
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction t = session.beginTransaction();
		
		Query query = session.createQuery("from Usuario");
		List<Usuario> lista = query.list();
		t.commit();
		
		if(session.isOpen())
			session.close();
		
		return lista;
		
	}
}
